package com.example.handle;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import com.example.controller.GirlController;
import com.example.domain.Result;
import com.example.exception.GirlException;
import com.example.util.ResultUtil;

/**
 * 
* @ClassName: ExceptionHandle 
* @Description: TODO(异常处理) 
* @author 强哥 
* @date 2017年12月12日 下午5:37:47 
*
 */
@ControllerAdvice//对控制层进行拦截
public class ExceptionHandle {
	private final static Logger logger = LoggerFactory.getLogger(ExceptionHandle.class);
	//Exception.class这类异常会在以下方法拦截，并以输出流方式输出
	@ExceptionHandler(value=Exception.class)
	@ResponseBody
	public Result handle(Exception e){
		if(e instanceof GirlException){
			GirlException ge = (GirlException) e;
			return ResultUtil.error(ge.getCode(), ge.getMessage());
		}else{
			logger.error("[系统异常]{}",e);
			return ResultUtil.error(-1, "未知错误");
		}
		
	}
}
